import random
import time

import requests

from common.dbutils import DBUtils
from config import URLPATH


class LoginRegister:
    # 获取图⽚验证码
    def get_imgverify(self, session, r):
        return session.get(url=f"{URLPATH}/common/public/verifycode1/{r}")

    # 获取短信验证码
    def get_smsverify(self, session, phone, imgVerifyCode, type='reg'):
        return session.post(url=f"{URLPATH}/member/public/sendSms",
                            headers={"Content-Type": "application/x-www-form-urlencoded"},
                            data={"phone": phone, "imgVerifyCode": imgVerifyCode, "type": type})

    # 注册
    def reg_data(self, session, phone, password, verifycode, phone_code, dy_server='on', invite_phone=None):
        return session.post(url=f"{URLPATH}/member/public/reg",
                            headers={"Content-Type": "application/x-www-form-urlencoded"},
                            data={"phone": phone, "password": password, "verifycode": verifycode,
                                  "phone_code": phone_code, "dy_server": dy_server, "invite_phone": invite_phone})

    # 登录信息
    def login_data(self, session,keywords,password):
        return session.post(url=f'{URLPATH}/member/public/login',
                      headers={'Content-Type':'application/x-www-form-urlencoded'},
                      data={'keywords':keywords,'password':password})

    # 是否登录
    def is_login(self,session):
        return session.post(url=f'{URLPATH}/member/public/islogin')


if __name__ == '__main__':
    '''
    200：登录成功， {"status": 200, "description": "登录成功"}
    100：⽤户不存在， {"status": 100, "description": "⽤户不存在"}
    100：密码不能为空， {"status": 100, "description": "密码不能为空"}
    100：密码错误1次， {"status": 100, "description": "密码错误1次,达到3次将锁定账户"}
    100：密码错误2次， {"status": 100, "description": "密码错误2次,达到3次将锁定账户"}
    100：密码错误3次， {"status": 100, "description": "由于连续输⼊错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录"}
    '''
    lr = LoginRegister()
    # session = session()
    session = requests.session()
    r = random.uniform(0,1)
    # 生成图片验证码
    print(lr.get_imgverify(session, r).text)

    # 处理手机号唯一性问题-数据库操作
    phone = 18888888888
    password = '123456'
    imgcode = 8888
    phone_code = 666666
    db = DBUtils()
    db.execute_sql(f"delete from mb_member where phone={phone};")
    db.execute_sql(f"delete from mb_member_info where member_name='{phone}';")
    db.execute_sql(f"delete from mb_member_login_log where member_name='{phone}';")
    db.execute_sql(f"delete from mb_member_register_log where phone={phone};")
    # 生成短信验证码
    print(lr.get_smsverify(session, phone, imgcode).json())
    # 注册信息
    print(lr.reg_data(session, phone, password, imgcode, phone_code).json())
    # 登录信息
    print(lr.login_data(session, '18888888888', '123456').json())
    # 是否登录
    print(lr.is_login(session).json())
    # print(lo.login_data('13956487123', '123456').json())
    # print(lo.login_data('18888888888', '').json())
    # print(lo.login_data('18888888888', '12345').json())
    # print(lo.login_data('18888888888', '1234').json())
    # print('-'*200)
    # pwd = '12345'
    # for i in range(5):
    #     if i == 4:
    #         pass
    #         # pwd = '123456'
    #     response = lo.login_data('18888888888', pwd).json()
    #     if 'data' not in response and i < 4:
    #         pass
    #         # time.sleep(60)
    #     print(response)
    #
    # print(lo.is_login().json())